package inline.calendario;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class ListaCompromissos extends ListActivity {
	public static final int CRIAR_ID = Menu.FIRST;
	public static final int CONSULTAR_ID = Menu.FIRST + 1;
	public static final int REMOVER_ID = Menu.FIRST + 2;
	
	private CalendarioService servico;
	private Cursor c;
	
	
	Bundle extras;
	TextView mData;
	
	// date and time
	private int mYear;
	private int mMonth;
	private int mDay;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
    	super.onCreate(icicle);
    }

	public void onResume() {
		super.onResume();
		setContentView(R.layout.listacompromissos);
		mData = (TextView) findViewById(R.id.labelData);
		try {
			extras = getIntent().getExtras();
			if (extras != null) {
				mDay = extras.getInt("DIA");
				mMonth = extras.getInt("MES");
				mYear = extras.getInt("ANO");
				mData.setText(mDay + "/" + mMonth + "/" + mYear);
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		servico = new CalendarioService(this);
        servico.abrirBanco();
        fillData();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		stopManagingCursor(c);
		servico.fecharBanco();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, CRIAR_ID, R.string.menu_criar);
        menu.add(0, CONSULTAR_ID, R.string.menu_consultar);
        menu.add(0, REMOVER_ID, R.string.menu_remover);
        return result;
    }

    
    
    @Override
    public boolean onOptionsItemSelected(Item item) {
    	switch (item.getId()) {
        case CRIAR_ID:
            servico.criarCompromisso(mYear, mMonth, mDay);
            return true;
        case CONSULTAR_ID:
        	try {
    			if (c.count() > 0){
    				servico.consultarCompromisso(c.getLong(0), c);
    			}
    			else
    				Toast.makeText(this, "Nenhum compromisso!", Toast.LENGTH_SHORT).show();
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return true;
        case REMOVER_ID:
        	try {
        			if (c.count() > 0){
        				servico.removerCompromisso(c.getLong(0));
        				fillData();
        			}
        			else
        				Toast.makeText(this, "Nenhum compromisso!", Toast.LENGTH_SHORT).show();
				} catch (RuntimeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private void fillData() {
		// Get all of the notes from the database and create the item list
    	c = servico.getMDbHelper().fetchAllNotesFromDate(mData.getText().toString());
    	//c = servico.getMDbHelper().fetchAllNotes();
		startManagingCursor(c);

		String[] from = new String[] {
				CalendarioDbAdapter.KEY_HORA,				
				CalendarioDbAdapter.KEY_TITLE,					
				CalendarioDbAdapter.KEY_AVISO };
		int[] to = new int[] { 
				R.id.hora,
				R.id.text1,				
				R.id.aviso };
		// Now create an array adapter and set it to display using our row
		SimpleCursorAdapter compromissos = new SimpleCursorAdapter(this,R.layout.notes_row, c, from, to);
		setListAdapter(compromissos);
	}
}
